package cn.lf.android.lawfirm.undertake.apply;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import cn.lf.android.lawfirm.BaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.downloadcontroller.modle.Agreement;
import cn.lf.android.lawfirm.undertake.apply.controler.ApplyUnderConroller;
import cn.lf.android.lawfirm.undertake.apply.controler.ApplyUnderInfoConroller;
import cn.lf.android.lawfirm.undertake.apply.controler.CaseTypeConroller;
import cn.lf.android.lawfirm.undertake.apply.modle.Approver;
import cn.lf.android.lawfirm.undertake.apply.modle.BasicFee;
import cn.lf.android.lawfirm.undertake.apply.modle.CaseLevel;
import cn.lf.android.lawfirm.undertake.apply.modle.CaseType;
import cn.lf.android.lawfirm.undertake.apply.modle.CaseTypeTwo;
import cn.lf.android.lawfirm.undertake.apply.modle.UndertakeApply;
import cn.lf.android.lawfirm.undertake.apply.modle.UndertakeApplyInfo;
import cn.lf.android.lawfirm.undertake.approval.AddLawyerFeeActivity;
import cn.lf.android.lawfirm.undertake.approval.modle.LawyerFee;
import cn.lf.android.lawfirm.undertake.approval.modle.LawyerFeeC;
import cn.lf.android.lawfirm.uploadcontroller.UpLodeController;
import cn.lf.android.lawfirm.uploadcontroller.view.UpAgreementAdapter;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.timepicker.TimePicker;
import cn.lf.android.lawfirm.view.NoScrollListView;
import cn.lf.android.lawfirm.view.pickerview.OptionsPopupWindow;

import static cn.jiguang.c.a.n;

public class ApplyUndertakeInfoActivity extends BaseActivity implements ApplyUnderInfoConroller.ApplyUnderInfoCallback {

    /**
     * 返回
     **/
    private ImageButton img_left;
    /**
     * 头
     **/
    private TextView mtitle;
    //--合同文件
    private List<Agreement> mListFileInfo = new ArrayList<Agreement>();
    private NoScrollListView file_list;
    private UpAgreementAdapter mAgreementAdapter;
    //---
    private String approvesid = "";
    private TextView caset_type, lawyer_fee, name, slient, opposing_party, matter, team, charge_mode, pay_way, lawyerfee, baselawyer_fee, choice_level, time_lawfee, approver;

    private TextView total, fee_remarks, apply_remarks, time, benchmark_cost;
    private AutoRelativeLayout rl_lawyer_fee;

    //------参数----
//    private String entry_name = "";//款项名称
//    private String shoufei_time = "";//收费时间
//    private String shoukuan_tiaojian = "";//收款条件
    private String approveman = "";//审批人编号
    private String sid = "";
    private String caseTypeID = "";//案件类型（必填）
    private ImageView add_file;
    private ArrayList<String> options1_major = new ArrayList<String>();
    private ArrayList<ArrayList<String>> options2_major = new ArrayList<ArrayList<String>>();
    private ApplyUnderInfoConroller mApplyUnderInfoConroller;

    @Override
    public Object getContentResView() {
        return R.layout.activity_apply_undertake_info;
    }

    @Override
    public void initView() {
        initTitle();

        sid = getIntent().getStringExtra("sid");
        if (null == sid) {
            showCToast("sid为null，请查看数据库");
        }
        rl_lawyer_fee = (AutoRelativeLayout) findViewById(R.id.rl_lawyer_fee);
        rl_lawyer_fee.setOnClickListener(this);
        name = (TextView) findViewById(R.id.name);
        slient = (TextView) findViewById(R.id.slient);
        opposing_party = (TextView) findViewById(R.id.opposing_party);
        matter = (TextView) findViewById(R.id.matter);
        team = (TextView) findViewById(R.id.team);
        charge_mode = (TextView) findViewById(R.id.charge_mode);
        pay_way = (TextView) findViewById(R.id.pay_way);
        lawyerfee = (TextView) findViewById(R.id.lawyerfee);
        baselawyer_fee = (TextView) findViewById(R.id.baselawyer_fee);
        choice_level = (TextView) findViewById(R.id.choice_level);
        time_lawfee = (TextView) findViewById(R.id.time_lawfee);
        approver = (TextView) findViewById(R.id.approver);
        caset_type = (TextView) findViewById(R.id.caset_type);
        lawyer_fee = (TextView) findViewById(R.id.lawyer_fee);
        total = (TextView) findViewById(R.id.total);
        fee_remarks = (TextView) findViewById(R.id.fee_remarks);
        apply_remarks = (TextView) findViewById(R.id.apply_remarks);
        time = (TextView) findViewById(R.id.time);
        benchmark_cost = (TextView) findViewById(R.id.benchmark_cost);
        add_file = (ImageView) findViewById(R.id.add_file);
        add_file.setVisibility(View.GONE);
        file_list = (NoScrollListView) findViewById(R.id.file_list);
        mAgreementAdapter = new UpAgreementAdapter(ApplyUndertakeInfoActivity.this, mListFileInfo, "承办申请查看");
        file_list.setAdapter(mAgreementAdapter);
        setlistener();
    }


    private void initTitle() {
        img_left = (ImageButton) findViewById(R.id.img_left);
        img_left.setVisibility(View.VISIBLE);
        img_left.setOnClickListener(this);
        mtitle = (TextView) findViewById(R.id.eb_tv_title);
        mtitle.setVisibility(View.VISIBLE);
        mtitle.setText("承办申请");
    }

    public void setlistener() {

        benchmark_cost.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String ss = s.toString().trim();
                String sss = time.getText().toString().trim();
                if (ss.length() > 0 && sss.length() > 0) {
                    time_lawfee.setText("" + Integer.valueOf(ss) * Integer.valueOf(sss));
                }
            }
        });
        time.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String ss = s.toString().trim();
                String sss = benchmark_cost.getText().toString().trim();
                if (ss.length() > 0 && sss.length() > 0) {
                    time_lawfee.setText("" + Integer.valueOf(ss) * Integer.valueOf(sss));
                }
            }
        });
    }

    @Override
    public void initData() {
        //滑动退出
//        addSwipeFinishLayout();
        mApplyUnderInfoConroller = new ApplyUnderInfoConroller(this, this);
        mApplyUnderInfoConroller.onApplyUnderInfo(sid);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_lawyer_fee:
                hintKbTwo();
                Intent intent1 = new Intent(ApplyUndertakeInfoActivity.this, AddLawyerFeeActivity.class);
                LawyerFeeC b = new LawyerFeeC();
                b.bean = mbeans.Payment;
                intent1.putExtra("lawyerfee", b);
                intent1.putExtra("from", "ApplyUndertakeInfoActivity");
                startActivity(intent1);
                break;
            case R.id.img_left:
                hintKbTwo();
                finish();
                break;
            default:
                break;
        }
    }

    private UndertakeApplyInfo mbeans;

    @Override
    public void onApplyUnderInfouccessed(UndertakeApplyInfo beans) {
        DebugLog.e("beans==" + beans);
        mbeans = beans;
        setView(beans);
    }

    public void setView(UndertakeApplyInfo beans) {
        name.setText(beans.caseName);//案件名称
        slient.setText(beans.WeiTuoFang);//委托人
        opposing_party.setText(beans.dfdsr);//对方当事人
        matter.setText(beans.commitment);//委托事项
        team.setText(beans.members);//承办成员
        total.setText(beans.contractInfo.contractAmount);//合同总额
        charge_mode.setText(beans.contractInfo.chargeType);//收费方式
        pay_way.setText(beans.contractInfo.payWay);//付款方式
        baselawyer_fee.setText(beans.contractInfo.basicFees);//基本律师费
        fee_remarks.setText(beans.contractInfo.contractRemark);//收费备注
        caset_type.setText(beans.CaseType.typeName);   //案件类别
        choice_level.setText(beans.CaseLevel.levelName);    //选择审级
        approver.setText(beans.Approver.userName);   //审批人
        apply_remarks.setText(beans.applyRemark);
        benchmark_cost.setText(beans.commitment);
        mListFileInfo.addAll(beans.attachments);
        mAgreementAdapter.notifyDataSetChanged();

    }

    @Override
    public void onApplyUnderInfoFailed(String errorMsg) {
        showCToast(errorMsg);
    }


}
